package com.googlecode.dgwt.client.dojo.data.format;

import com.googlecode.dgwt.client.dijit.Dijit;
import com.googlecode.dgwt.client.dojo.exception.ValidationException;

public class DoubleFormatter extends BaseFormatter<Double> {

    private static DoubleFormatter formatter;
    
    protected DoubleFormatter() {
    }
    
    @Override
    public String formatValue(Double value) {
        return value == null ? null : formatDouble(value);
    }

    @Override
    public Double unformatValue(String value) throws ValidationException {
        try {
            return value == null || (value=value.trim()).isEmpty() ? null : Double.valueOf(unformatDouble(value));
        } catch (NumberFormatException e) {
            throw new ValidationException(Dijit.MESSAGE_RESOURCES.valueInvalid(value, "{0}")) ;
        }
    }

    public static DoubleFormatter get() {
        if (formatter == null) {
            formatter = new DoubleFormatter();
        }
        return formatter;
    }

}
